package com.cefa.edu.service;

import com.cefa.edu.dao.ClassMapper;
import com.cefa.edu.dao.OrganRelationMapper;
import com.cefa.edu.dao.UserOrganRoleMapper;
import com.cefa.edu.pojo.Class;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by cc664 on 2018/8/15.
 * @aa classService
 */
@Service
public class ClassService {

    @Autowired
    ClassMapper classMapper;
    @Autowired
    OrganRelationMapper organRelationMapper;
    @Autowired
    UserOrganRoleMapper userOrganRoleMapper;

    /**
     * @aa 添加班级
     * @param schoolId
     * @param phaseId
     * @param gradeId
     * @param className
     * @return
     */
    public int saveClass(Integer schoolId, Integer phaseId, Integer gradeId, String className) {

        /**
         * 班级添加：
         班级表添加数据
         机构关系表
         select * from cefa_edu_organ_relation
         参数：
         班级名称 年级id  学校id 学习阶段id
         */
          //添加班级数据
        Map<String,Object> classMap=new HashMap<String,Object>();
        Class classBean=new Class();
        classBean.setSchoolId(schoolId);
        classBean.setClassName(className);
        classMapper.saveClass(classBean);
        //添加班级、年级、学段、学校关系数据
        Map<String,Object> organRelMap=new HashMap<String,Object>();
        organRelMap.put("schoolId",schoolId);
        organRelMap.put("phaseId",phaseId);
        organRelMap.put("gradeId",gradeId);
        organRelMap.put("classId",classBean.getId());
        organRelationMapper.saveOrganRelationForClass(organRelMap);

        return 0;
    }

    /**
     * @aa 根据班级id 删除班级
     * @aa add by chaijian 2018-08-15
     * @param classId
     * @return
     */
    @Transactional
    public void deleteClass(Integer classId) {
        organRelationMapper.deleteOrganRelationByClassId(classId);
        userOrganRoleMapper.deleteUserOrganRoleByClassId(classId);
        classMapper.deleteClassByIds(classId+"");

    }
}
